<?php
class Product extends Ecore_Db {
	var $cache;
	var $clear;
	public function __construct() {
		$this->cache = new Ecore_Cache();
		$this->clear = $this->cache->clear;
	}
	public function get($intProductId) {
		try {						
			$this->prepare('call products_get(?)');		
			$this->bindParam(1, $intProductId, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();
			return isset($arrResult[0]) ? $arrResult[0] : null;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Product::get - ' .$e->getMessage());			
			
		}
	}	
	public function getAll($intAdtype, $intStatus, $intCategoryId, $intSubId, $intOption, 
						   $strKey, $strStart, $strEnd, $intOff, $intLimit, 
						   &$intTotal=0) {
		try {				
			$this->prepare('call products_getall_a(?,?,?,?,?,
												 ?,?,?,?,?
												)');		
			$this->bindParam(1 , $intAdtype		, PDO::PARAM_INT, 4);			
			$this->bindParam(2 , $intStatus		, PDO::PARAM_INT, 4);
			$this->bindParam(3 , $intCategoryId	, PDO::PARAM_INT, 11);
			$this->bindParam(4 , $intSubId		, PDO::PARAM_INT, 11);			
			$this->bindParam(5 , $intOption		, PDO::PARAM_INT, 4);
			$this->bindParam(6 , $strKey		, PDO::PARAM_STR, 50);
			$this->bindParam(7 , $strStart		, PDO::PARAM_STR, 10);
			$this->bindParam(8 , $strEnd		, PDO::PARAM_STR, 10);
			$this->bindParam(9 , $intOff		, PDO::PARAM_INT, 11);
			$this->bindParam(10, $intLimit		, PDO::PARAM_INT, 6);
			$arrResult 	= $this->fetchAll();
			$intTotal	= isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Product::getAll - ' .$e->getMessage());			
			
		}
	}
	public function add($intAdminId, $intCategoryId, $intSubId, $intManufactoryId, $intOriginId,
						$strName, $strShort, $strContent, $strTag, $strImage,
						$strPrice, $strFeatures, $intAlbumId) {
		try {
			$intProductType	= PRODUCT_TYPE;
			$this->prepare('call products_add(?,?,?,?,?,
										      ?,?,?,?,?,
										      ?,?,?,?
										)');
			$this->bindParam(1 , $intAdminId		, PDO::PARAM_INT, 11);			
			$this->bindParam(2 , $intCategoryId		, PDO::PARAM_INT, 11);
			$this->bindParam(3 , $intSubId			, PDO::PARAM_INT, 11);
			$this->bindParam(4 , $intManufactoryId	, PDO::PARAM_INT, 11);
			$this->bindParam(5 , $intOriginId		, PDO::PARAM_INT, 11);
			$this->bindParam(6 , $strName			, PDO::PARAM_STR, 255);
			$this->bindParam(7 , $strShort			, PDO::PARAM_STR, 255);
			$this->bindParam(8 , $strContent		, PDO::PARAM_STR);
			$this->bindParam(9 , $strTag			, PDO::PARAM_STR, 255);
			$this->bindParam(10, $strImage			, PDO::PARAM_STR, 150);
			$this->bindParam(11, $strPrice			, PDO::PARAM_STR, 20);
			$this->bindParam(12, $strFeatures		, PDO::PARAM_STR);
			$this->bindParam(13, $intAlbumId		, PDO::PARAM_INT, 11);
			$this->bindParam(14, $intProductType	, PDO::PARAM_INT, 4);			
			$arrResult 		= $this->fetchAll();		
			$intError		= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			$intProductId 	= isset($arrResult[0]['productid']) ? $arrResult[0]['productid'] : 0;
			if($intError == 0) {				
				$objSearch		= new Ecore_Search();
				$strSlug		= Ecore_Rewrite_Product::slug($intProductId,$strName);
				$objSearch->addUpdate($intProductId, $intProductType, $strName, '', $strTag,
									  $strShort, $strImage, $strSlug, 0, 0,
									  Ecore_Function::now(), 1, 1, $intCategoryId, $intSubId);
			}
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Product::add - ' . $e->getMessage());
			
		}
	}		
	public function update($intAdminId, $intProductId, $intCategoryId, $intSubId, $intManufactoryId, 
						   $intOriginId, $strName, $strShort, $strContent, $strTag, 
						   $strImage, $strPrice, $strFeatures, $intAlbumId) {
		try {
			$intProductType	= PRODUCT_TYPE;
			$this->prepare('call products_update(?,?,?,?,?,
												 ?,?,?,?,?,
												 ?,?,?,?,?
												)');
			$this->bindParam(1 , $intAdminId		, PDO::PARAM_INT, 11);			
			$this->bindParam(2 , $intProductId		, PDO::PARAM_INT, 11);			
			$this->bindParam(3 , $intCategoryId		, PDO::PARAM_INT, 11);
			$this->bindParam(4 , $intSubId			, PDO::PARAM_INT, 11);
			$this->bindParam(5 , $intManufactoryId	, PDO::PARAM_INT, 11);
			$this->bindParam(6 , $intOriginId		, PDO::PARAM_INT, 11);
			$this->bindParam(7 , $strName			, PDO::PARAM_STR, 255);
			$this->bindParam(8 , $strShort			, PDO::PARAM_STR, 255);
			$this->bindParam(9 , $strContent		, PDO::PARAM_STR);
			$this->bindParam(10 , $strTag			, PDO::PARAM_STR, 255);
			$this->bindParam(11, $strImage			, PDO::PARAM_STR, 150);
			$this->bindParam(12, $strPrice			, PDO::PARAM_STR, 20);
			$this->bindParam(13, $strFeatures		, PDO::PARAM_STR);
			$this->bindParam(14, $intAlbumId		, PDO::PARAM_INT, 11);
			$this->bindParam(15, $intProductType	, PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();		
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {				
				$strOldImage = isset($arrResult[0]['image']) ? $arrResult[0]['image'] : '';
				if($strImage != '') {
					$this->deleteImg($strOldImage);
				}
				$objSearch		= new Ecore_Search();				
				$strSlug		= Ecore_Rewrite_Product::slug($intProductId,$strName);
				$objSearch->addUpdate($intProductId, $intProductType, $strName, '', $strTag,
									  $strShort, $strImage, $strSlug, 0, 0,
									  Ecore_Function::now(), 1, 1, $intCategoryId, $intSubId);
			}
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Product::update - ' . $e->getMessage());
			
		}
	}	
	public function updateStatus($intAdminId, $intProductId, $intStatus, $intPerm) {
		try {				
			$this->prepare('call products_update_status(?,?,?,?)');
			$this->bindParam(1, $intAdminId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intProductId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intStatus, PDO::PARAM_INT, 4);
			$this->bindParam(4, $intPerm, PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$objSearch = new Ecore_Search();
				$objSearch->updateStatus($intProductId, 5, $intStatus);
			}
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Product::updateStatus - ' .$e->getMessage());			
			
		}
	}
	public function delete($intAdminId, $intProductId) {
		try {			
			$this->prepare('call products_delete(?,?)');
			$this->bindParam(1, $intAdminId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intProductId, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {				
				$strImage = isset($arrResult[0]['image']) ? $arrResult[0]['image'] : '';
				if($strImage != '') {
					$this->deleteImg($strImage);
				}
			}
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Product::delete - ' .$e->getMessage());			
			
		}
	}
	public function deleteImg($strImg) {
		if(file_exists(DIR_PRODUCT_FULL . $strImg))
			@unlink(DIR_PRODUCT_FULL . $strImg);
		if(file_exists(DIR_PRODUCT_THUMB . $strImg))
			@unlink(DIR_PRODUCT_THUMB . $strImg);
		if(file_exists(DIR_PRODUCT_SMALL . $strImg))
			@unlink(DIR_PRODUCT_SMALL . $strImg);
	}	
}